import logging

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base

try:
    from ..config import POSTGRESQL_URI
except ImportError:
    # PostgreSQL
    POSTGRESQL = {
        'user': 'work',
        'password': 'W3lcome!X',
        'host': '127.0.0.1',
        'port': 5432,
        'dbname': 'fleague',
    }
    POSTGRESQL_URI = "postgresql+psycopg2://{user}:{password}@{host}:{port}/{dbname}".format(**POSTGRESQL)

engine = create_engine(POSTGRESQL_URI, client_encoding='utf8', echo=True)

session_factory = sessionmaker(bind=engine)

Base = declarative_base()


def create_scoped_session():
    return scoped_session(session_factory)
